﻿/*
* 作 者: 槐尧
* 公 司: 墨宇天城
* 创建时间: 2016/3/3 10:22:26
* 说明:  
*/

using System.Collections;
using System.IO;
using System;

public class FileManager
{
    /// <summary>
    /// 读文件
    /// </summary>
    /// <param name="path"></param>
    /// <returns></returns>
    public static string ReadFile(string path)
    {
        string str = "";
        try
        {
            str = File.ReadAllText(path);
        }
        catch (IOException e)
        {
            Console.WriteLine(e.ToString());
        }

        return str;
    }

    /// <summary>
    /// 写文件
    /// </summary>
    /// <param name="path">路径</param>
    /// <param name="content">内容</param>
    /// <returns></returns>
    public static bool WriteBytes(string path,string content)
    {
        try
        {
            string finalPath = path.Replace("\\", "/");
            int last = finalPath.LastIndexOf("/");
            string folderPath = finalPath.Substring(0, last);
            if (!Directory.Exists(folderPath))
            {
                Directory.CreateDirectory(folderPath);
            }

            FileStream fs = new FileStream(path, FileMode.Create);
            //获得字节数组
            byte[] data = System.Text.Encoding.UTF8.GetBytes(content);
            //开始写入
            fs.Write(data, 0, data.Length);
            //清空缓冲区、关闭流
            fs.Flush();
            fs.Close();

            return true;
        }
        catch (System.Exception ex)
        {
            Console.WriteLine(ex.ToString());
            return false;
        }
        
    }
}